Skip to content

avoid SpecifiedTypes allocation in setRootExpr when unchanged#5326

Closed
SanderMuller wants to merge 1 commit intophpstan:2.1.xfrom
SanderMuller:2.1.x
Closed

avoid SpecifiedTypes allocation in setRootExpr when unchanged#5326
SanderMuller wants to merge 1 commit intophpstan:2.1.xfrom
SanderMuller:2.1.x

Conversation

@SanderMuller
Copy link
Copy Markdown
Contributor

Problem

SpecifiedTypes::setRootExpr() is called on the return value of nearly every specifyTypesInCondition() call in TypeSpecifier. Each call allocates a new SpecifiedTypes object, even when the root expression is already the same object.

Since specifyTypesInCondition recurses through the AST and calls setRootExpr($expr) at each level, inner calls often set the root to a node that an outer call then overwrites with the same node again. The same Expr object reference is passed down and back up the call chain, so identity (===) is the right check here.

Solution

public function setRootExpr(?Expr $rootExpr): self
{
    if ($this->rootExpr === $rootExpr) {
        return $this;
    }
    // ... existing allocation
}

Impact

CI benchmark results to follow.

@phpstan-bot
Copy link
Copy Markdown
Collaborator

You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x.

@SanderMuller SanderMuller changed the base branch from 2.2.x to 2.1.x March 29, 2026 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants